package com.fbinggun.fbinggunutil;



import com.alibaba.fastjson.JSONObject;
import com.fbinggun.sql.Formula;
import com.fbinggun.sql.SQLite;
import com.fbinggun.sql.Table;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

//MySQL数据表
//以ID ,Json 格式储存数据到数据库中
//可适用于绝大部分插件的数据储存

/**
 * MySQL数据表 常用与 数据库保存玩家数据
 */

public class JsonTable {

    Table table;
    public JsonTable(SQLite sql, String tableName) throws SQLException, ClassNotFoundException {
        table = new Table(sql,tableName);
        if(!table.IsTableNull()){
            table.CreateTable(
                    new Formula("ID", "VARCHAR", 199),
                    new Formula("Json", "text")
            );
        }
    }

    @Deprecated
    public String getJson(String player) throws SQLException, ClassNotFoundException {
        Object o= table.getObject("ID",player,"Json");
        if(o==null){
            return null;
        }
        String json = (String) o;
        if(json.equalsIgnoreCase("")||json.equalsIgnoreCase("null")){
            return null;
        }
        return json;
    }

    public JSONObject getJSONObject(String player) throws SQLException, ClassNotFoundException {
        Object o= table.getObject("ID",player,"Json");
        if(o==null){
            return null;
        }
        String json = (String) o;
        if(json.equalsIgnoreCase("")||json.equalsIgnoreCase("null")){
            return null;
        }
        return JSONObject.parseObject(json);
    }

    @Deprecated
    public Map<String,String> getJsonAll() throws SQLException, ClassNotFoundException {
        String t = "select * from "+table.getTablename();
        if(table.getSql().IsConnection()){
            table.getSql().connection();
        }
        Map<String,String> map =new HashMap<>();
        Statement statement=    table.getSql().getStatement();
        ResultSet s =statement.executeQuery(t);
        while (s.next()){
            String id =s.getString("ID");
            String json=s.getString("Json");
            map.put(id,json );
        }
            s.close();
            statement.close();
            return map;
    }

    @Deprecated
    public Map<String,JSONObject> getJSONObjectAll() throws SQLException, ClassNotFoundException {
        String t = "select * from "+table.getTablename();
        if(table.getSql().IsConnection()){
            table.getSql().connection();
        }
        Map<String,JSONObject> map =new HashMap<>();
        Statement statement=    table.getSql().getStatement();
        ResultSet s =statement.executeQuery(t);
        while (s.next()){
            String id =s.getString("ID");
            String json=s.getString("Json");
            if(json!=null) {
                map.put(id, JSONObject.parseObject(json));
            }
        }
        s.close();
        statement.close();
        return map;
    }

    @Deprecated
    public void setJson(String player,String json) throws SQLException, ClassNotFoundException {
        table.setObject("ID",player,"Json",json);
    }

    public void setJson(String player, JSONObject json) throws SQLException, ClassNotFoundException {
        table.setObject("ID",player,"Json",json.toJSONString());
    }
    public void delJson(String player) throws SQLException, ClassNotFoundException {
        table.del("ID",player);
    }
}
